package httpService

import (
	"github.com/labstack/echo/v4"
	"pm/db"
	"pm/model"
	"strconv"
)

func PackageList(c echo.Context)error {
	//获取参数
	name := c.FormValue("name")
	//current := c.FormValue("current")
	pageSizeParam := c.FormValue("page_size")
	pageParam := c.FormValue("page")
	pageSize := 10
	page := 1
	if pageSizeParam != ""{
		pageSize,_ = strconv.Atoi(pageSizeParam)
	}
	if pageParam != ""{
		page,_ = strconv.Atoi(pageParam)
	}
	var (
		PackageData []model.PackageHistory
		Total int64
	)
	if name != ""{
		db.NewDb().Model(&model.PackageHistory{}).
			Where("name = ?", name).
			Limit(pageSize).
			Offset((page - 1) * pageSize).
			Order("created_at desc").
			Count(&Total).
			Find(&PackageData)

	}else{
		db.NewDb().Model(&model.PackageHistory{}).
			Limit(pageSize).
			Offset((page - 1) * pageSize).
			Order("created_at desc").
			Count(&Total).
			Find(&PackageData)
	}

	return c.JSON(200, map[string]interface{}{
		 "list": PackageData,
		 "total":Total,
	})
}
